package com.tj.jinzao.api;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.widget.ProgressBar;

import com.tj.jinzao.R;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.utils.Trace;
import com.tj.jinzao.utils.Utils;

import rx.Subscriber;

/**
 */
public abstract class SubscriberCallBack<T> extends Subscriber<ResultResponse<T>> {
    private Context mContext;
    protected static Dialog sDialog;

    public SubscriberCallBack(Context context) {
        this.mContext = context;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (this.mContext != null && isShowProgress() && this.mContext instanceof Activity) {
            showProgress(mContext);
        }
    }

    /**
     * 是否展示进度框浮层
     *
     * @return
     */
    protected boolean isShowProgress() {
        return false;
    }

    @Override
    public void onNext(ResultResponse response) {
        closeProgress();
        if (Utils.parseInt(response.getCode()) == 0) {
            onSuccess((T) response.getData());
            if (response.getPi() != null) {
                onPageSuccess(response.getPi().getCurrentPage(), response.getPi().getTotalPage(), response.getPi().getTotalSize());
            }
        } else {
            onFailure(response);
        }
        if(response.getFace_photo_url()!=null){
            onStringSuccess(response.getFace_photo_url(),response.getTongue_photo_url());
        }
        onStringNull(response);
        i("数据更新添加","888888"+response.toString());

    }
    public static void i(String tag, String msg) {  //信息太长,分段打印
        //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
        //  把4*1024的MAX字节打印长度改为2001字符数
        int max_str_length = 2001 - tag.length();
        //大于4000时
        while (msg.length() > max_str_length) {
            Log.i(tag, msg.substring(0, max_str_length));
            msg = msg.substring(max_str_length);
        }
        //剩余部分
        Log.i(tag, msg);
    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        closeProgress();
        e.printStackTrace();
        Trace.i("http", "onError错误！" + e.toString());
    }


    protected abstract void onSuccess(T response);


    protected void onPageSuccess(int currentPage, int allPage, int allSize) {

    }
    protected void onStringSuccess(String face_photo_url,String tongue_photo_url) {

    }
    protected void onStringNull(ResultResponse msg) {

    }

//    protected abstract void onError();

    protected abstract void onFailure(ResultResponse response);


    /**
     * 网络连接时显示Loading
     */
    public static void showProgress(Context context) {
        if (sDialog == null && context instanceof Activity) {
            try {
                sDialog = new Dialog(context, R.style.ThemeRequestDialog);
                sDialog.setContentView(new ProgressBar(context));
                sDialog.setCancelable(true);
                sDialog.setCanceledOnTouchOutside(false);
                sDialog.show();
            } catch (Exception e) {
            }
        }
    }

    public static void closeProgress() {
        try {
            if (sDialog != null) {
                sDialog.dismiss();
                sDialog = null;
            }
        } catch (Exception e) {
        }
    }

}
